﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IndyMosaic.Models.Services;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;


namespace IndyMosaic.Models.Services.Facebook
{
    public class Membership : IMembershipService
    {
        private string GetUID(string jsonResponse)
        {
            JObject o = JObject.Parse(jsonResponse);
            string uid = (string)o.SelectToken("id");
            return uid;
        }
        

        public T GetFacebookObject<T>(string objectId, string accessToken)
        {
            string baseUrl = string.Format("https://graph.facebook.com/{0}?access_token={1}", objectId, accessToken);
            HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(baseUrl);
            HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
            string jsonResponse = string.Empty;
            using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
            {
                jsonResponse = sr.ReadToEnd();
            }
            try
            {
                T fbObject = JsonConvert.DeserializeObject<T>(jsonResponse);
                return fbObject;
            }
            catch
            {
                return default(T);
            }
        }

        public string JsonResult(object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        #region IMembershipService Members

        public int MinPasswordLength
        {
            get { throw new NotImplementedException(); }
        }

        public bool ValidateUser(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                return false;
            }
            User user = GetFacebookObject<User>("me", password);
            if (user != null)
            {
                return user.ID == userName;
            }
            return false;
        }

        public System.Web.Security.MembershipCreateStatus CreateUser(string userName, string password, string confirmPassword, string emailAddress,
            string fullName, string reminderQuestion, string reminderAnswer)
        {
            throw new NotImplementedException();
        }

        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            throw new NotImplementedException();
        }

        public string ResetPassword(string userName)
        {
            throw new NotImplementedException();
        }

        public System.Web.Security.MembershipUserCollection GetAllUsers(Guid supplierID)
        {
            throw new NotImplementedException();
        }

        public string GetMembershipCreateStatusInfo(System.Web.Security.MembershipCreateStatus status)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
